#define   __LIBRARY__
#include <unistd.h>
#include <linux/sched.h>

#define SHM_COUNT 20

struct struct_shm_tables
{
	int key;
	unsigned long addr;
    char occupied;
} shm_tables[SHM_COUNT];

int last_shm = 0;

int sys_shmget(int key){
    int i= 0;
    if(last_shm >= SHM_COUNT) return -1;
    for(i=0;i<last_shm;i++)
        if((shm_tables[i].occupied == 1) && (shm_tables[i].key == key)) return i;
    if(!(shm_tables[last_shm].addr = get_free_page())) return -1;
    shm_tables[last_shm].key = key;
    shm_tables[last_shm].occupied = 1;
    return last_shm++;
}
void * sys_shmat(int shmid){
    if((shmid >= SHM_COUNT) && (shm_tables[shmid].occupied != 1)) return -1;
    unsigned long v_addr = current->brk + shmid * PAGE_SIZE;
	put_page(shm_tables[shmid].addr,current->start_code + v_addr);
	return (void*)v_addr;
}